package org.sloth.core.kv
{
	/**
	 * 键值对管理器<br/>
	 * <b>Note</b>：键值对有分组概念(<code>catalog</code>)
	 */	
	public class KVMgr
	{
//============================================================================
//PROPERTY
//============================================================================
		private static const _catalogLibraryMap:Object = {};
//============================================================================
//PUBLIC
//============================================================================
		/**
		 * 添加一个键值对集合字符串配置
		 * @param catalog 该键值对集合对应的key值
		 * @param property 键值对集合字符串
		 * @param regLine 行注释正则表达式
		 * @param regSegment 段注释正则表达式
		 */		
		public static function addProperty(catalog:String, property:String, regLine:RegExp = null, regSegment:RegExp = null):void
		{
			if (catalog == null || catalog == "" || property == null) return;
			_catalogLibraryMap[catalog] = new KVLibrary(property, regLine, regSegment);
		}
		/**
		 * 删除一个键值对集合
		 * @param catalog 键值对集合对应的key值
		 */		
		public static function removeProperty(catalog:String):void
		{
			if (!hasProperty(catalog)) return;
			delete _catalogLibraryMap[catalog];
		}
		/**
		 * 判断指定Key的键值对集合是否存在
		 * @param catalog 键值对集合对应的key值
		 * @return 
		 */		
		public static function hasProperty(catalog:String):Boolean
		{
			return catalog in _catalogLibraryMap;
		}
		/**
		 * 获取指定key对应的value
		 * @param key 要获取的value的key值
		 * @param catalog 键值对集合对应的key值。如果不为null，则在对应键值对集合中查找，否则在所有键值对集合中查找并返回第1个
		 * @return 
		 */		
		public static function getValue(key:String, catalog:String = null):*
		{
			var library:KVLibrary = _catalogLibraryMap[catalog];
			if (library != null)
			{
				if (library == null) return null;
				return library[key];
			}
			for (catalog in _catalogLibraryMap)
			{
				library = _catalogLibraryMap[catalog];
				if (key in library) return library[key];
			}
			return null;
		}
	}
}